package com.projectbox.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

import com.projectbox.dao.ProjectDao;
import com.projectbox.dao.ResourceDao;
import com.projectbox.dao.SearchDao;
import com.projectbox.dao.UserDao;
import com.projectbox.model.Project;
import com.projectbox.model.Resource;
import com.projectbox.service.SearchService;
import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implements the SearchService Interface
 * @author Pavel
 *
 */
public class SearchServiceImpl implements SearchService{
	
	private SearchDao searchDao;
	private UserDao userDao;
	private ProjectDao projectDao;
	private Logger logger = LoggerFactory.getLogger(getClass()); 
	
	public void setSearchDao(SearchDao searchDao){
		this.searchDao = searchDao;
	}
	
	public void setUserDao (UserDao userDao){
		this.userDao = userDao;
	}
	
	public void setProjectDao (ProjectDao projectDao){
		this.projectDao = projectDao;
	}
	/**
	 * Searches the Project Names for a Match
	 * @param keyword the search string 
	 * @param startDate the initial point of search
	 * @param endDate the final point of search
	 * TODO: checking the date attributes
	 */
	public List<Project> searchProjects(long uid, String keyword, String startDate,
			String endDate) {
		
		Date sDate = new Date();
		Date eDate = new Date();
		logger.debug("startDate in search service: " + startDate);
		logger.debug("endDate in search service: " + endDate);
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {	
			sDate = (Date) formatter.parse(startDate);
			eDate = (Date) formatter.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return  setProjectStatus(uid,setProjectOwner(searchDao.searchProjects(keyword, sDate,eDate)));
	}

	public List<Resource> searchResources(String keyword, String startDate,
			String endDate) {
		
		Date sDate = new Date();
		Date eDate = new Date();
		logger.debug("startDate in search service: " + startDate);
		logger.debug("endDate in search service: " + endDate);
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {	
			sDate = (Date) formatter.parse(startDate);
			eDate = (Date) formatter.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return setProperties(searchDao.searchResources(-1, keyword, sDate,eDate));
	}
	
	public List<Resource> searchMyResources(long userId, String keyword, String startDate, String endDate){
		
		Date sDate = new Date();
		Date eDate = new Date();
		logger.debug("startDate in search service: " + startDate);
		logger.debug("endDate in search service: " + endDate);
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {	
			sDate = (Date) formatter.parse(startDate);
			eDate = (Date) formatter.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return setProperties(searchDao.searchResources(userId, keyword, sDate, eDate));
	}
	
	public List<Resource> searchResourceWithTypes(String type, String startDate, String endDate){
		
		Date sDate = new Date();
		Date eDate = new Date();
		logger.debug("startDate in search service: " + startDate);
		logger.debug("endDate in search service: " + endDate);
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {	
			sDate = (Date) formatter.parse(startDate);
			eDate = (Date) formatter.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return setProperties(searchDao.searchResourcesWithType(type, sDate, eDate));
	}
	
	public List<Resource> setProperties(List<Resource> resourceList) {
		if (resourceList != null) {
			for (Resource r : resourceList) {
				r.setOwner(this.userDao.getUserById(r.getUid()));
				r.setProject(this.projectDao.getProjectById(r.getPid()));
				r.setLastModifier(this.userDao.getUserById(r.getLastModId()));
			}
		}
		return resourceList;
	}
	
	public List<Project> setProjectOwner(List<Project> projectList){
		if(!projectList.isEmpty()){
			for (Project p: projectList){
				p.setManager(this.userDao.getUserById(p.getMid()));
			}
		}
		return projectList;
	}
	
	public List<Project> setProjectStatus(Long uid, List<Project> projects){
		
		for (Project p : projects){
			String status = projectDao.getAccessStatus(uid, p.getId());
			p.setUserStatus(status);
		}
		
		return projects;
	}
	
	public List<String> getResourceTypes() {
		// TODO Auto-generated method stub
		return null;
	}

}
